<template>
  <div class="dialog-container">
    <!-- 选择商品弹窗 -->
    <el-dialog
      title="选择商品"
      :visible.sync="visible"
      :modal-append-to-body="false"
    >
      <!-- 搜索区域 -->
      <div class="search">
        <el-input size="small" />
        <el-button type="primary" size="small">搜索</el-button>
      </div>
      <!-- table区域 -->
      <!-- v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)" -->
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        ref="table"
        :data="list"
        row-key="id"
        border
        fit
        highlight-current-row
        :show-overflow-tooltip="true"
        size="small"
        style="width: 100%"
        @select="selectHandler"
        @select-all="selectAllHandler"
        @current-change="currentChangeHandler"
      >
        <el-table-column
          align="center"
          :type="multiple ? 'selection' : 'index'"
          width="60"
        >
        </el-table-column>
        <el-table-column align="left" label="商品">
          <template v-slot="{row}">
            <div class="goods-item table">
              <div class="goods-item__image">
                <img :src="row.icon" alt="" />
              </div>
              <div class="goods-item__info">
                <div class="goods-item__info__name" :title="row.name">{{row.name}}</div>
                <div class="goods-item__info__price">
                  <span>￥{{row.price}}</span>
                  <span>500人付款</span>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div class="page">
        <el-pagination
          @current-change="pageChangeHandler"
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="totalNum">
        </el-pagination>
      </div>
      <!-- 操作区域 -->
      <div class="button">
        <el-button type="primary" size="small" @click="confirmHandler">确定</el-button>
        <el-button type="danger" size="small" @click="handleDialog">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryGoodsList } from '../../service/queryGoodsList'
export default {
  name: 'SelectGoods',
  model: {
    prop: 'show',
    event: 'input'
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      loading: false,
      visible: false,
      list: [], // 分页获取的列表
      selectedList: [], // 选中的
      currentPage: 1,
      pageSize: 5,
      totalNum: 0 // 数据总条数
    }
  },
  watch: {
    visible (val) {
      if (!val) {
        this.$emit('input', false)
      }
    },
    show (val) {
      if (val) {
        this.visible = true
      }
    }
  },
  created () {
    this.queryGoodsListHandle()
  },
  methods: {
    // 单选
    selectHandler (val) {
      this.selectedList = val
    },
    // 单选
    currentChangeHandler (val) {
      console.log('单选', val)
      if (!this.multiple) {
        this.selectedList = [val]
        this.confirmHandler()
      }
    },
    // 全选
    selectAllHandler (val) {
      this.selectedList = val
    },
    confirmHandler () {
      if (this.selectedList.length !== 0) {
        this.$emit('select', this.unrefs(this.selectedList))
      }
      this.visible = false
    },
    handleDialog () {
      this.visible = false
    },
    pageChangeHandler (page) {
      this.currentPage = page
      this.queryGoodsListHandle()
    },
    queryGoodsListHandle () {
      this.loading = true
      queryGoodsList (this.currentPage, this.pageSize).then(res => {
        this.loading = false
        this.list = res.data
        this.totalNum = res.pageInfo ? (res.pageInfo.total || 0) : 0
      }).catch(() => {
        this.loading = false
      })
    },
    unrefs (data) {
      const checkType = item => Object.prototype.toString.call(item)
      const unref = data => {
        let tmp
        if (checkType(data) === '[object Object]') {
          tmp = {}
          const keys = Object.keys(data)
          for (let i = 0, l = keys.length; i < l; i++) {
            tmp[keys[i]] = unref(data[keys[i]])
          }
        } else if (checkType(data) === '[object Array]') {
          tmp = []
          for (let i = 0, l = data.length; i < l; i++) {
            tmp[i] = unref(data[i])
          }
        } else {
          tmp = data
        }
        return tmp
      }
      return unref(data)
    }
  }
}
</script>

<style lang="less" scoped>
.dialog-container {
  .search {
    height: 50px;
    display: flex;
    align-items: center;
    ::v-deep .el-input {
      width: 400px;
      margin-right: 20px;
    }
  }
  ::v-deep {
    .el-dialog__body {
      padding: 20px;
    }
  }
  .goods-item {
    margin-top: 10px;
    height: 60px;
    display: flex;
    align-items: center;
    border: 1px dashed #409eff;
    box-sizing: border-box;
    padding-right: 20px;
    background-color: #eef5fe;
    &__image {
      margin-left: 10px;
      width: 40px;
      height: 40px;
      img {
        width: 40px;
        height: 40px;
      }
    }
    &__info {
      height: 40px;
      width: 530px;
      &__name {
        margin-left: 10px;
        height: 20px;
        line-height: 20px;
        font-size: 14px;
        color: #333;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      &__price {
        margin-left: 20px;
        height: 20px;
        span:first-child {
          font-size: 12px;
          color: #f90;
          line-height: 20px;
        }
        span:last-child {
          line-height: 20px;
          margin-left: 10px;
          font-size: 12px;
          color: #9b9b9b;
        }
      }
    }
    &.table {
      margin-top: 0;
      background-color: inherit;
      border: none;
      .goods-item__info {
        width: 640px;
      }
    }
  }
}
</style>